/**
 * 
 */
package xx.teach.entity;

import java.sql.SQLException;
import java.sql.Timestamp;

import javax.sql.RowSet;

import xx.db.QueryRunner;
import xx.db.annotation.Column;
import xx.db.annotation.TableName;
import xx.db.sql.Owner;
import xx.db.sql.Table;

/**
 * @author Administrator 2014-3-18
 */
@TableName(name = "t_question")
public class Question extends Table
{

    /**
     * @param qr
     * @param owner
     */
    public Question(QueryRunner qr, Owner owner)
    {
        super(qr, owner);
        // TODO Auto-generated constructor stub
    }

    public static final String QUESTION_ID = "question_id";

    @Column(name = QUESTION_ID, pk = true)
    private long questionId;

    public static final String ACCOUNT_ID = "account_id";

    @Column(name = ACCOUNT_ID)
    private long accountId;

    public static final String SUBMIT_TIME = "submit_time";

    @Column(name = SUBMIT_TIME)
    private Timestamp submitTime;

    public static final String SEQUENCE = "sequence";

    @Column(name = SEQUENCE)
    private int sequence;

    public static final int DOWN_CLASS_QUESTION = 2;

    public static final String TYPE = "type";

    @Column(name = TYPE)
    private int type;

    public static final String CONTENT = "content";

    @Column(name = CONTENT)
    private String content;

    public static final String ATTACH_PATH = "attach_path";

    @Column(name = ATTACH_PATH)
    private String attachPath;

    public static final String TEACHER_ID = "teacher_id";

    @Column(name = TEACHER_ID)
    private long teacherId;

    public static final String FLAG = "flag";

    @Column(name = FLAG)
    private int flag; // 1=teacher;2=student

    public static final String READED = "readed";

    @Column(name = READED)
    private boolean readed;

    public long getQuestionId()
    {
        return questionId;
    }

    public Question setQuestionId(long questionId)
    {
        this.questionId = questionId;
        return this;
    }

    public long getAccountId()
    {
        return accountId;
    }

    public Question setAccountId(long accountId)
    {
        this.accountId = accountId;
        return this;
    }

    public Timestamp getSubmitTime()
    {
        return submitTime;
    }

    public Question setSubmitTime(Timestamp submitTime)
    {
        this.submitTime = submitTime;
        return this;
    }

    public int getSequence()
    {
        return sequence;
    }

    public Question setSequence(int sequence)
    {
        this.sequence = sequence;
        return this;
    }

    public int getType()
    {
        return type;
    }

    public Question setType(int type)
    {
        this.type = type;
        return this;
    }

    public String getContent()
    {
        return content;
    }

    public Question setContent(String content)
    {
        this.content = content;
        return this;
    }

    public String getAttachPath()
    {
        return attachPath;
    }

    public Question setAttachPath(String attachPath)
    {
        this.attachPath = attachPath;
        return this;
    }

    public long getTeacherId()
    {
        return teacherId;
    }

    public Question setTeacherId(long teacherId)
    {
        this.teacherId = teacherId;
        return this;
    }

    public int getFlag()
    {
        return flag;
    }

    public Question setFlag(int flag)
    {
        this.flag = flag;
        return this;
    }

    public boolean isReaded()
    {
        return readed;
    }

    public Question setReaded(boolean readed)
    {
        this.readed = readed;
        return this;
    }

    /*
     * (non-Javadoc)
     * @see xx.db.sql.Table#insert()
     */
    @Override
    public long insert() throws SQLException
    {
        int sequence = getNextSequence() + 1;
        this.setSequence(sequence);
        return super.insert();
    }

    /**
     * 获取sequence
     * 
     * @return
     * @throws SQLException
     */
    private int getNextSequence() throws SQLException
    {
        RowSet rs = select.from(Question.class, "max(" + Question.SEQUENCE + ")").where(Question.ACCOUNT_ID + "=" + accountId).query();
        rs.next();
        return rs.getInt(1);
    }

    /**
     * 根据accountId获取问题列表
     * 
     * @return
     * @throws SQLException
     */
    public RowSet getQuestionByAccountId() throws SQLException
    {
        return select.from(Question.class).leftJoin(Account.class, Account.NAME).using(ACCOUNT_ID).where(Question.ACCOUNT_ID + "=" + accountId).desc(Question.SUBMIT_TIME).query();
    }

    /**
     * 获取未读信息列表（老师用的）
     * 
     * @return
     * @throws SQLException
     */
    public RowSet getAccountByReaded() throws SQLException
    {
        return select.from(Account.class, Account.NAME, Account.ACCOUNT_ID).leftJoin(Question.class).using(ACCOUNT_ID)
                .where(Question.READED + "=" + false + " and " + Question.FLAG + "=2 ").query();
    }

    public int updateReaded() throws SQLException
    {
        return update.update(Question.class).set(Question.READED, true).where(Question.ACCOUNT_ID + "=" + accountId).go();
    }
}
